using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;

namespace Elte.SqlSpIdx.Indices
{
    /// <summary>
    /// This class stands for Balanced K-dimensional Tree 
    /// Includes tree building, nearest neighbour searching and
    /// value estimating based on nearest neighbours
    /// </summary>
    public class NoIndex : Index
    {

        /// <summary>
        /// Constructor with Sqlconnection
        /// </summary>
        /// <param name="cn"></param>
        public NoIndex(SqlConnection cn)
            : base(cn)
        {
            type = IndexType.NoIndex;
        }

        /// <summary>
        /// Constructor with SqlTransaction
        /// </summary>
        /// <param name="tn"></param>
        public NoIndex(SqlTransaction tn)
            : base(tn)
        {
            type = IndexType.NoIndex;
        }

        public NoIndex()
            : base()
        {
            type = IndexType.NoIndex;
        }


        public override void CreateIndex(string name, string description, string table, string primaryKeyColumn, string indexColumn, string dataColumn, string dataColumnNames, string options)
        {
            NoIndex index = null;
            bool newtransaction = false;
            if (databaseTransaction == null)
            {
                databaseTransaction = databaseConnection.BeginTransaction();
                newtransaction = true;
            }

            try
            {
                index = new NoIndex(databaseTransaction);
                index.Name = name;
                index.Description = description;
                index.Table = table;
                index.PrimaryKeyColumn = primaryKeyColumn;
                index.DataColumn = dataColumn;
                index.DataColumnNames = dataColumnNames;
                index.Options = options;
                index.IndexColumn = indexColumn;
                index.GetMinMax();
                

                index.Save();
                Core.Log("Index saved...\r\n");



            }
            catch (Exception ex)
            {
                Core.Log("Error: " + ex.Message + "\r\n");
            }
            finally
            {
                if (newtransaction) databaseTransaction.Commit();
            }
        }

        public override int LookupIndex(double[] coord)
        {
            return 0;
        }

        public override IEnumerable NearestNeighborsIndex(double[] coord, int N)
        {
            string distancecolumn = "";
            for (int i = 0; i < coord.Length; i++)
            {
                if (i != 0) distancecolumn += "+";
                distancecolumn += ("((" + this.DataColumnNameForSelect(i) + "-(" + (coord[i].ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat)) + "))*(" + this.DataColumnNameForSelect(i) + "-(" + coord[i].ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + ")))");
            }

            string sql = "";//temp Elte.SqlSpIdx.Indices.None.NoneScripts.None_NearestNeighbor;
            sql = sql.Replace("$count", N.ToString());
            sql = sql.Replace("[$Primarykey]", this.PrimaryKeyColumn);
            sql = sql.Replace("[$Datatable]", this.Table);
            sql = sql.Replace("[$order]", distancecolumn);

            System.Collections.Generic.List<long> result = new List<long>();
            using (SqlCommand cmd = new SqlCommand(sql, DatabaseConnection))
            {
                SqlDataReader reader=cmd.ExecuteReader();
                while (reader.Read())
                {
                    result.Add((long)reader[0]);
                }
            }

            return result;
            
        }

        public override IEnumerable SpherePoints(double[] coord, double R)
        {
            throw new Exception("The method or operation is not implemented.");
        }



        public override IEnumerable<object[]> SpatialQuery(string selectlist, string spatialwhere, string sqlwhere)
        {
            throw new NotImplementedException();
        }
    }

}
